<?php

class Photo_Controller_Browse_Ajax_Add extends Nuo_Controller
{
	function execute($request)
	{
		$resourceId = $request->getParam('resource-item');

		if ($resourceId)
		{
			$resource = Nuo::getItem($resourceId);
		}

		if (!$resource)
		{
			$resource = Nuo::getUser();
		}

		$table = Nuo::getTable('photo');

		$poster = Nuo::getUser();

		$uploads = Nuo::getApi('core/storage')->FILES('atms');
		
		$files = array();
		
	
		if ($uploads && isset($uploads['photo']))
		{
			$files = Nuo::getApi('core/storage')->processImages($uploads['photo'], 'photo');
		}

		$counter = 0;

		/**
		 * first select
		 */
		$mainPhoto = null;
		$photos = array();

		foreach ($files as $file)
		{
			$item = $table -> fetchNew();
			$item -> setPhotoFile($file);
			$item -> created_at = time();
			$item->setResource($resource)->setPoster($poster);				
			$item -> save();
			$photos[] = $item;
			if (NULL == $mainPhoto)
			{
				$mainPhoto = $item;
			}
			++$counter;
		}

		/**
		 * add activity feed to action
		 */

		if (is_object($mainPhoto))
		{
			$privacy = array('value' => $data['privacy']);
			$feed = Nuo::getApi('activity') -> addFeed($poster, $resource, $mainPhoto, 'photo:addNew', $_POST['privacy']);
		}

		if ($feed)
		{
			$attatchment = Nuo::getApi('core/attachment') -> addAttachments($feed, $photos);
		}

		
		return  'parent.nuo.refresh();';
	}

}
